package tv_megazineplus
import org.codehaus.groovy.runtime.TimeCategory
import groovy.time.TimeDuration
import fb.FbUtil
import groovy.sql.Sql

class SendAlertJob {
    static triggers = {
    simple name: 'sendMyAlerts', startDelay: 10000, repeatInterval: 10000  
  }
    def SendMailService
    def SendSmsService
    def FbService        
    
    def execute() {
        
        def userNumber
        def email  
        def alerts
        def start, end, end2, rp
        TimeDuration td
        
        use(TimeCategory){
            alerts = Alert.findByHoraDisparaBetween(new Date(), new Date() + 5.minutes)
        }
            
        alerts.each{
                
            userNumber = it.user.phoneNumber
            email = it.user.email
                
            rp = it.program.prog_start
            td = TimeCategory.minus( rp, new Date())
                
            if(it.sent == 0){
                //SMS
                if(it.tipo == 'sms'){
                    start = System.currentTimeMillis()
                    sendSmsService.serviceMethod("TvMagP", userNumber, 
                        "O programa <" + it.program.getProgrammationName() + "> do canal <" + it.program.prog_channel.getChannel2() + 
                        "> começa daqui a " + td + "\n <<" + it.personalMessage + ">>")
                    
                    it.sent=1
                    it.save()
                    end = System.currentTimeMillis()
                    end2 = new Date()
                    new Statistic(servico: Servicos.findByNameLike('Serviço de Alertas - SMS'), totalTime: end-start, date: end2).save()
                    //EMAIL    
                }else{
                    start = System.currentTimeMillis()  
                    SendMailService.serviceMethod(email, "Alerta TvMagazinePlus", "O programa <" + it.program.getProgrammationName() + "> do canal <" + it.program.prog_channel.getChannel2() + 
                        "> começa daqui a " + td + "\n <<" + it.personalMessage + ">>")
                    
                    it.sent=1
                    it.save()
                    end = System.currentTimeMillis()
                    end2 = new Date()
                    new Statistic(servico: Servicos.findByNameLike('Serviço de Alertas - EMAIL'), totalTime: end-start, date: end2).save()
                }
                def all = UserPrefs.findByUser(it.user)
                if(all.postMural == true){
                //facebook here/*
                FbUtil fb = new FbUtil()        
                if(it.user.fbtoken.size()>15){
                    def content = 'TvMAGAzine Plus: ' + it.user.name + ' recommends <' + it.program.prog_title + '> on <' + it.program.prog_channel + '>\n Starts in about ' + td + '\n\nEnjoy!'
                    fb.sendComent(it.user.fbtoken, content)
                }
            }
                }else{
                //doNothing()
            }
        }
                             
        
    }
}